import DebugExt from "../base/DebugExt";
import MutiLanguage from "../base/MutiLanguage";

const { ccclass, property, menu } = cc._decorator;

@ccclass
@menu('GlobalUI/MutiLanguageText')
export default class MutiLanguageText extends cc.Component {

    @property
    private readonly key: string = "";

    protected onLoad(): void {
        this.fetchRender();
        yy.event.on(this, yy.event.lang.LanuageChanged, this.updateText);
    }

    protected onEnable(): void {
        this.updateText();
    }

    private fetchRender() {
        this._Label = this.getComponent(cc.Label);
        if (!this._Label)
            this._Label = this.getComponent(cc.RichText);
    }
    private _Label: cc.Label | cc.RichText = null;

    private updateText() {
        if (!this._Label) {
            DebugExt.warn("MutiLanguageText updateText _Label is null, nodeName: " + this.node.name + " key: " + this.key);
            return;
        }
        if (!this._Label.isValid || !this.node.activeInHierarchy) return;
        this._Label.string = MutiLanguage.getIns().getString(this.key);
    }
}
